//
//  YKSelectView.m
//  YunKang
//
//  Created by leoking870 on 2017/9/29.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSelectView.h"
#import "YKSelectDropdownView.h"
#import "YKDropdownLabel.h"
@interface YKSelectView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) YKDropdownLabel *dropdownLabel;
@property (nonatomic, strong) id<YKDropdown> initialText;

@end


@implementation YKSelectView

- (instancetype)initWithTitle:(NSString *)title row:(NSArray<YKDropdown>*)data initialText:(id<YKDropdown>)initialText
{
    self = [super init];
    if (self) {
        _contentTopInset = 8;
        _disableBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [self addSubview:self.titleLabel];
        self.titleLabel.tf_weight = 1;
        self.data = data;
        self.initialText = initialText;
        self.titleLabel.text = title;
        
        [self addSubview:self.dropdownLabel];
        self.dropdownLabel.tf_weight = 3;
        self.dropdownLabel.backgroundColor = [UIColor whiteColor];
        [self.subviews mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@8 leadingTo:self.mas_leading leadSpace:13 trailingTo:self.mas_trailing tailSpace:13];
        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).offset(8);
            make.centerY.equalTo(self.mas_centerY);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title row:(NSArray<YKDropdown> *)data initialText:(id<YKDropdown>)initialText weights:(NSArray *)weights {
    self = [super init];
    if (self) {
        _contentTopInset = 8;
        _disableBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [self addSubview:self.titleLabel];
        self.titleLabel.tf_weight = [weights.firstObject integerValue];
        self.data = data;
        self.initialText = initialText;
        self.titleLabel.text = title;
        
        [self addSubview:self.dropdownLabel];
        self.dropdownLabel.tf_weight = [weights.lastObject integerValue];
        self.dropdownLabel.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [self.subviews mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@8 leadingTo:self.mas_leading leadSpace:13 trailingTo:self.mas_trailing tailSpace:13];
        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).offset(_contentTopInset);
            make.centerY.equalTo(self.mas_centerY);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

- (void)setContentTopInset:(CGFloat)contentTopInset {
    _contentTopInset = contentTopInset;
    [self.subviews mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(contentTopInset);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(30);
    }];
}

- (id<YKDropdown>)selectedRow {
    return self.dropdownLabel.text;
}
- (void)setSelectedRow:(id<YKDropdown>)selectedRow {
    self.dropdownLabel.text = selectedRow;
}

- (void)setEnabled:(BOOL)enabled {
    _dropdownLabel.enabled = enabled;
    _dropdownLabel.backgroundColor = enabled?[UIColor whiteColor]:_disableBackgroundColor;
    _dropdownLabel.layer.cornerRadius = 4.f;
    _dropdownLabel.layer.masksToBounds = YES;
    _dropdownLabel.layer.borderWidth = .5f;
    _dropdownLabel.layer.borderColor = [UIColor tf_colorWithHexString:@"#ECECEC"].CGColor;
}

- (void)setDisableBackgroundColor:(UIColor *)disableBackgroundColor {
    _disableBackgroundColor = disableBackgroundColor;
}


- (BOOL)enabled {
    return _dropdownLabel.enabled;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor tf_colorWithHexString:@"#444444"];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.text = @"默认";
    }
    return _titleLabel;
}

- (YKDropdownLabel *)dropdownLabel {
    if (!_dropdownLabel) {
        _dropdownLabel = [[YKDropdownLabel alloc]initWithText:self.initialText row:self.data selectionHandler:^(YKDropdownLabel *view, NSInteger index, id<YKDropdown> text) {
            if (self.selectHandler) {
                self.selectHandler(text);
            }
        }];
    }
    return _dropdownLabel;
}

@end
